package com.example.photography.adapter.community;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.photography.R;
import com.example.photography.bean.CommunityBean;
import com.example.photography.ui.CircleImageView;
import com.example.photography.util.Util;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CommunityAdapter extends RecyclerView.Adapter {

    /**
     * 标签
     */
    public static final int LAB = 0;

    /**
     * 活动
     */
    public static final int ACT = 1;

    /**
     *问题
     */
    public static final int Que = 2;
    //类型
    private int currentType = 0;
    private Context context;
    private CommunityBean.DataBean dataBean;
    /**
     * 初始化布局
     */
    private final LayoutInflater layoutInflater;

    public CommunityAdapter(Context context, CommunityBean.DataBean dataBean) {
        this.context = context;
        this.dataBean = dataBean;
        layoutInflater = LayoutInflater.from(context);
    }

    /**
     * 相当于getView 创建viewHolder部分代码
     *
     * @param parent
     * @param viewType 当前的类型
     * @return
     */
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == LAB) {
            return new LabViewHolder(context, layoutInflater.inflate(R.layout.item_community_lab, null));
        } else if (viewType == ACT) {
            return new ActHolder(context, layoutInflater.inflate(R.layout.item_community_act, null));
        } else if (viewType == Que) {
            return new QueHolder(context, layoutInflater.inflate(R.layout.item_community_question, null));
        }
        return null;
    }


    /**
     * 得到类型
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case LAB:
                currentType = LAB;
                break;
            case ACT:
                currentType = ACT;
                break;
            case Que:
                currentType = Que;
                break;
        }
        return currentType;
    }

    /***
     * 相当于getView中的绑定数据模块
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Log.e("onBindViewHolder", "onBindViewHolder-----");
        if (getItemViewType(position) == LAB) {
            LabViewHolder labViewHolder = (LabViewHolder) holder;
            labViewHolder.setData(dataBean.getLabel());
        } else if (getItemViewType(position) == ACT) {
            ActHolder actHolder = (ActHolder) holder;
            actHolder.setData(dataBean.getActivity());
        } else if (getItemViewType(position) == Que){
            QueHolder queHolder = (QueHolder) holder;
            queHolder.setData(dataBean.getUser(),dataBean.getHot_comment());
        }
    }

    /**
     * 总共有多少个item类型
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return 3;
    }

    /**
     * 标签
     */
    class LabViewHolder extends RecyclerView.ViewHolder {
        private final Context context;
        private RecyclerView rc_rab;

        public LabViewHolder(Context context, View view) {
            super(view);
            this.context = context;
            rc_rab = view.findViewById(R.id.rc_lab);
            /**
             * 设置为横向RecyclerView
             */
            //设置RecyerView保持固定大小，可优化性能
            //创建LinearLayoutManager
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            //RecyclerView设置为横向滑动
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            //设置创建LinearLayoutManager到RecyclerView中
            rc_rab.setLayoutManager(linearLayoutManager);

        }


        public void setData(List<CommunityBean.DataBean.LabelBean> label) {
            Community_LabelAdapter community_labelAdapter = new Community_LabelAdapter(label, context);
            rc_rab.setAdapter(community_labelAdapter);
        }
    }

    /**
     * 活动
     */
    class ActHolder extends RecyclerView.ViewHolder {
        private final Context context;
        private RecyclerView rc_rab;
        private TextView more;


        public ActHolder(Context context, View view) {
            super(view);
            this.context = context;
            rc_rab = view.findViewById(R.id.rc_lab);
            more = view.findViewById(R.id.tv_more);
            /**
             * 设置为横向RecyclerView
             */
            //设置RecyerView保持固定大小，可优化性能
            //创建LinearLayoutManager
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            //RecyclerView设置为横向滑动
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            //设置创建LinearLayoutManager到RecyclerView中
            rc_rab.setLayoutManager(linearLayoutManager);

            more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Util.show(context,"position=more");
                }
            });
        }

        public void setData(List<CommunityBean.DataBean.ActivityBean> activity) {
            Community_ActAdapter community_actAdapter = new Community_ActAdapter(context,activity);
            rc_rab.setAdapter(community_actAdapter);
        }
    }
    class QueHolder extends RecyclerView.ViewHolder{
        private final Context context;
        private RelativeLayout ll_question;
        private CircleImageView cv_head;
        private RecyclerView rc_rab;

        public QueHolder(Context context, View view) {
            super(view);
            this.context = context;
            ll_question = view.findViewById(R.id.rl_question);
            cv_head = view.findViewById(R.id.cv_head);
            rc_rab = view.findViewById(R.id.rc_lab);
            /**
             * 设置为横向RecyclerView
             */
            //设置RecyerView保持固定大小，可优化性能
            //创建LinearLayoutManager
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            //RecyclerView设置为横向滑动
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            //设置创建LinearLayoutManager到RecyclerView中
            rc_rab.setLayoutManager(linearLayoutManager);
        }

        public void setData(CommunityBean.DataBean.UserBean user, List<CommunityBean.DataBean.HotCommentBean> hot_comment) {
            Glide.with(context)
                    .load(user.getHead_url())
                    .thumbnail(Glide.with(context).load(R.drawable.head_empty))
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                    .into(cv_head);

            ll_question.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Util.show(context,user.getId_unique());
                }
            });
            Community_QuuAdapter community_quuAdapter = new Community_QuuAdapter(context,hot_comment);
            rc_rab.setAdapter(community_quuAdapter);
        }
    }
}
